package com.nowcoder.community.controller;

import com.nowcoder.community.entity.User;
import com.nowcoder.community.service.ForgetPasswordService;
import com.nowcoder.community.service.UserService;
import com.nowcoder.community.util.CommunityUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

@Controller
public class ForgetPasswordController {

    @Autowired
    private UserService userService;

    @Autowired
    private ForgetPasswordService forgetPasswordService;

    @Value("${server.servlet.context-path}")
    private String contextPath;

    // 获取忘记密码页面
    @RequestMapping(value = "/forget-password", method = RequestMethod.GET)
    public String getForgetPasswordPage() {
        return "/site/forget";
    }

    // 忘记密码时发送验证码到邮箱，异步请求
    @RequestMapping(value = "/forget-password/code", method = RequestMethod.GET)
    @ResponseBody
    public String sendForgetPasswordCode(String email, HttpServletResponse response) {
        // 根据邮箱判断系统中是否存在该用户
        User user = userService.findUserByEmail(email);
        if (user == null) {
            return CommunityUtil.getJSONString(1, "该邮箱尚未注册帐号！");
        }

        // 发送cookie给客户端
        String emailAndCodeOwner = forgetPasswordService.sendForgetPasswordCode(email);
        Cookie cookie = new Cookie("emailAndCodeOwner", emailAndCodeOwner);
        cookie.setPath(contextPath + "/forget-password/reset");
        cookie.setMaxAge(5 * 60);
        response.addCookie(cookie);

        return CommunityUtil.getJSONString(0, "验证码已发送至您的邮箱,请登录邮箱查看!");
    }

    // 重置密码
    @RequestMapping(value = "/forget-password/reset", method = RequestMethod.POST)
    public String resetPassword(Model model, String email, String verifyCode, String newPassword,
                                @CookieValue(value = "emailAndCodeOwner", required = false) String emailAndCodeOwner) {
        // 判断cookie是否已失效
        if (emailAndCodeOwner == null) {
            model.addAttribute("codeMsg", "验证码已失效！请重新获取");
            return "/site/forget";
        }

        // 重置密码
        Map<String, Object> map = forgetPasswordService.resetPassword(email, verifyCode, newPassword, emailAndCodeOwner);
        if (map.isEmpty()) {
            // 重置密码成功
            return "redirect:/login";
        } else {
            // 重置密码失败
            model.addAttribute("emailMsg", map.get("emailMsg"));
            model.addAttribute("codeMsg", map.get("codeMsg"));
            model.addAttribute("passwordMsg", map.get("passwordMsg"));
            return "/site/forget";
        }
    }

}
